package com.chookss.tiku

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.chookss.Urls
import com.chookss.tiku.entity.*
import com.chookss.tiku.response.BaseResponse
import com.chookss.tiku.response.PracticeSubmit
import com.chookss.tiku.response.SubjectDetailResponse
import com.chookss.tiku.util.TiKuUtils
import com.chookss.tools.httpForm

/**
 * Company:
 * Project:
 * Author: liuhaoge
 * Date: 2020/12/28
 * Note: 模拟试卷数据类
 */
abstract class BasePracticeTestViewModel() : SubjectAnswerViewModel() {
    //提交答案
    abstract fun submitAnswer(subject: Subject, answers:List<String>, time:Long) : LiveData<LiveResult<Any>>
    //收藏题目
    open fun collectSubject(subject: Subject, sourceType:String) = commonCollectSubject(subject, sourceType,"1")

    //取消收藏题目
    open fun unCollectSubject(subject: Subject, sourceType:String) = commonCollectSubject(subject, sourceType,"0")

    //取消收藏题目
    private fun commonCollectSubject(subject: Subject, sourceType:String, collectStatus:String):LiveData<LiveResult<Any>> {
        var liveResult = MutableLiveData<LiveResult<Any>>()

//        collectType	true	String	收藏类型 0 收藏视频 1 收藏题目
//        collectId	true	String	视频id或者题目编码
//        collectStatus	true	String	状态 0 取消 1 收藏

        val params2: MutableMap<String, Any?> = HashMap()
        params2["collectType"] = "1"
        params2["collectId"] = subject.subjectCode
        params2["collectStatus"] = collectStatus
        params2["sourceType"] = sourceType

        jobs + httpForm<BaseResponse> {
            url = "api/sys/my/toCollect"
            params = params2
            onSuccess = {
                subject.collectFlag = collectStatus
                liveResult.postValue(LiveResult(null))
            }
            onError = {
                liveResult.postValue(LiveResult(Error(it)))
            }
        }
        return liveResult
    }

    //接口描述：记录我的错题本数据，此接口作为用户通过“错题本”或者“我的收藏
    //”，进行答题时，答错的情况下使用
    protected fun submitErrorSubject(subject: Subject, answers:List<String>):LiveData<LiveResult<Any>> {
        var liveResult = MutableLiveData<LiveResult<Any>>()
        var cache = subjectAnswerCache[subject.subjectCode]
        if (cache == null) {
            liveResult.postValue(LiveResult(Error("选项列表为空")))
            return liveResult
        }

        if (answers.isEmpty()) {
            liveResult.postValue(LiveResult(Error("请先答题,再提交")))
            return liveResult
        }
        if(subject.subjectTypeCode.equals("1004")||subject.subjectTypeCode.equals("1005")){
        }else{
            //回答正确 不提交错误
            if (TiKuUtils.myAnswerIsRight(cache, answers)) {
                liveResult.postValue(LiveResult("1"))
                return liveResult
            }
        }
        val params2: MutableMap<String, Any?> = HashMap()
        params2["subjectCode"] = subject.subjectCode
//        params2["selectOption"] = answers.joinToString(",")
        if(subject.subjectTypeCode.equals("1004")){
            params2["selectOption"] = answers.joinToString("@#^")
        }else{
            params2["selectOption"] = answers.joinToString(",")
        }
        jobs + httpForm<PracticeSubmit.Response> {
            url = "api/practice/insertErrorSubject"
            params = params2
            onSuccess = {
                liveResult.postValue(LiveResult(it.data.isRight))

            }
            onError = {
                liveResult.postValue(LiveResult(Error(it)))
            }
        }
        return liveResult
    }
    //获取题目详情

    public fun getSubjectDetail(subject: Subject):LiveData<LiveResult<SubjectDetailEntity>> {
        var liveResult = MutableLiveData<LiveResult<SubjectDetailEntity>>()

        val params2: MutableMap<String, Any?> = HashMap()
        params2["subjectCode"] = subject.subjectCode
        jobs + httpForm<SubjectDetailResponse> {
            url = Urls.getSubjectSituation
            params = params2
            onSuccess = {
                liveResult.postValue(LiveResult(it.data))
            }
            onError = {
                liveResult.postValue(LiveResult(Error(it)))
            }
        }
        return liveResult
    }
}
